package com.example.springbootServiceNetwork.demos.web.Controller;

import com.example.springbootServiceNetwork.demos.web.Config.JwtInfo;
import com.example.springbootServiceNetwork.demos.web.Config.Result;
import com.example.springbootServiceNetwork.demos.web.Config.ResultEnum;
import com.example.springbootServiceNetwork.demos.web.Entity.Dict;
import com.example.springbootServiceNetwork.demos.web.Entity.DictItem;
import com.example.springbootServiceNetwork.demos.web.Service.DictService;
import com.example.springbootServiceNetwork.demos.web.Service.JwtRedistService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/dict")
public class DictController {

    @Resource
    private DictService dictService;

    @Resource
    private JwtRedistService jwtRedistService;


    /**
     * 字典查询(父类)
     */
    @GetMapping("/queryDict")
    public Result addDict(@RequestParam("child") String child,@RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
            return dictService.queryDictGet(child,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
            result.setData(e.getCause());
        }
        return result;
    }


    /**
     * 新增字典(父类)
     */
    @PostMapping("/addDict")
    public Result addDict(@RequestBody Dict dict,@RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
            return dictService.addDict(dict,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
            result.setData(e.getCause());
        }
        return result;
    }

    /**
     * 更新字典(父类)
     */
    @PostMapping("/updateDict")
    public Result updateDict(@RequestBody Dict dict,@RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
            return dictService.updateDict(dict,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
            result.setData(e.getCause());
        }
        return result;
    }

    /**
     * 删除字典(父类)
     */
    @PostMapping("/deleteDict")
    public Result deleteDict(@RequestBody Dict dict,@RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
            return dictService.deleteDict(dict,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
            result.setData(e.getCause());
        }
        return result;
    }


    /**
     * 字典查询(子类)
     */
    @PostMapping("/queryDict")
    public Result queryDict(@RequestBody Dict dict,@RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =new JwtInfo();
            if (!(token.isEmpty())){
                jwtInfo =jwtRedistService.getUserInfo(token);
            }else {
                jwtInfo.setPass(false);
            }
            return dictService.queryDict(dict,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.FORBIDDEN.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
        }
        return result;
    }


    /**
     * 新增字典(子类)
     */
    @PostMapping("/addDictItem")
    public Result addDictItem(@RequestBody DictItem dictItem, @RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
            return dictService.addDictItem(dictItem,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
            result.setData(e.getCause());
        }
        return result;
    }

    /**
     * 更新字典(子类)
     */
    @PostMapping("/updateDictItem")
    public Result updateDictItem(@RequestBody DictItem dictItem,@RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
            return dictService.updateDictItem(dictItem,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
            result.setData(e.getCause());
        }
        return result;
    }

    /**
     * 删除字典(子类)
     */
    @PostMapping("/deleteDictItem")
    public Result deleteDictItem(@RequestBody DictItem dictItem,@RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
            return dictService.deleteDictItem(dictItem,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
            result.setData(e.getCause());
        }
        return result;
    }


    /**
     *  自动生成字典编码
     */
    @PostMapping("/autoDictCode")
    public Result queryDict(@RequestBody DictItem dictItem,@RequestHeader("token") String token){
        Result result =new Result();
        try {
            JwtInfo jwtInfo =new JwtInfo();
            if (!(token.isEmpty())){
                jwtInfo =jwtRedistService.getUserInfo(token);
            }else {
                jwtInfo.setPass(false);
            }
            return dictService.autoDictCode(dictItem,jwtInfo);
        }catch (Exception e){
            result.setCode(ResultEnum.FORBIDDEN.getCode());
            result.setMsg(ResultEnum.FORBIDDEN.getData());
        }
        return result;
    }

}
